package zb.blog.base.util.qiniu.test;

import com.qiniu.util.Auth;
import org.junit.jupiter.api.Test;
import zb.blog.base.global.GlobalVar;
import zb.blog.base.util.StringUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Author: BinZhang
 * @Date: 2018/5/28 12:19
 * @Description:下载文件演示
 */
public class Download {
    @Test
    public void test() throws UnsupportedEncodingException {

        String fileName = "爱仕达.zip";
        String domainOfBucket = GlobalVar.QNY_DEMO_URL + "/demoZip/1527580778831.zip?attname=";
        String encodedFileName = URLEncoder.encode(fileName, "utf-8");
        String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
        int index = publicUrl.lastIndexOf("/");
        StringBuilder sb = new StringBuilder(publicUrl);
        sb.replace(index, index + 1, "");
        String accessKey = GlobalVar.QNY_ACCESS_KEY;
        String secretKey = GlobalVar.QNY_SECRET_KEY;
        Auth auth = Auth.create(accessKey, secretKey);
        long expireInSeconds = 10L;//可以自定义链接过期时间，10秒钟过期
        //String downloadRUL = auth.privateDownloadUrl(domainOfBucket, expireInSeconds);
        String downloadRUL = auth.privateDownloadUrl(sb.toString(), expireInSeconds);
        System.out.println(downloadRUL);
    }
}
